package com.qiao.glod.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qiao.glod.R;
import com.qiao.glod.util.NetUtils;

/**
 * Created by Qiao on 2016/4/7.
 * function：
 */
public class ContentFragment2 extends QFragment implements SwipeRefreshLayout.OnRefreshListener{
    SwipeRefreshLayout swipeRefreshLayout;
    String type ;
    int page;

    @Override
    protected int generateResId() {
        return R.layout.fragment_main;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        swipeRefreshLayout = findView(R.id.swipe);
        swipeRefreshLayout.setOnRefreshListener(this);

        bindRecyclerView();

        //判断有没有网络
        if (NetUtils.isNetWorkConection(context)) {
            //进来就自动刷新
            swipeRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(true);
                }
            });
        } else {
//            MyToast.showShortToast(getString(R.string.mm_no_net));
//            getDBDatas();
        }
    }

    private void bindRecyclerView(){
        RecyclerView recyclerView = findView(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                TextView textView = new TextView(getContext());
                textView.setPadding(20, 20, 20, 20);
                textView.setTextColor(Color.BLUE);
                return new ViewHolder(textView);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ((ViewHolder) holder).textView.setText("item" + position);
            }

            @Override
            public int getItemCount() {
                return 30;
            }
        });
    }


    @Override
    public void onRefresh() {
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(false);
            }
        },1000);
    }

    class ViewHolder extends  RecyclerView.ViewHolder {
        protected TextView textView;

        public ViewHolder(View view){
            super(view);
            this.textView = (TextView)view;
        }
    }
}
